package com.ebiz.jo.imageBrowser
{
	import org.fas.utils.FuNumber;
	
	
	public class NetCache
	{
		public var IB:ImageBrowser;
		//sum number of item
		public var numItems:int=103;
		private var imageArr:Array = new Array();
		//		private var arr:Array = new Array();
		public function NetCache()
		{
		}
		public function removeAll():void{
			//			arr = new Array();
			this.imageArr = new Array();
			this.numItems=10;
		}
		public function getClue(i:int):String{
			if(this.imageArr[i]){
				return this.imageArr[i].clue;
			}
			return '';
		}
		private var loadArea1:int = -1;
		private var loadArea2:int = -2;
		public function checkAndLoad(_currentIndex:int,_direction:int=1):void{
			var _area:int = int(_currentIndex/10);
			var _maxArea:int = int((numItems-1)/10);
			var _nextArea:int = FuNumber.nextStep(_area,_maxArea,0,1);
			var _lastArea:int = FuNumber.nextStep(_area,_maxArea,0,-1);
			if(!this.imageArr[_currentIndex]){
				this.buildItemArea(_area);
				var _n:int = FuNumber.nextInt(_area,_maxArea,0,2)
				this.removeItemArea(FuNumber.nextInt(_area,_maxArea,0,2));
				this.removeItemArea(FuNumber.nextInt(_area,_maxArea,0,-2));
			}
			//			if(_direction==1){//next
			//				if(!this.imageArr[_nextArea*10+1] && this.loadArea2!=_nextArea){
			//					//loadnext
			//				}
			//			}else if(_direction==-1){
			//				if(!this.imageArr[_lastArea*10-1] && this.loadArea2!=_nextArea){
			//					//loadnext
			//					this.lo
			//				}
			//			}
		}
		public function getItemAt(i:int):ImageContainer{
			if(i<0){
				i = this.numItems+i;
			}else if(i>=this.numItems){
				i = i-this.numItems;
			}
			this.checkAndLoad(i);
			if(this.imageArr[i]){
			}else{
				//				var _image:ImageContainer = new ImageContainer(this.IB.newImage(),this.IB.newLoadingMovie(),this.IB.newLoadingMovie());
				//				this.imageArr[i] = _image;
				//				_image.instance.name = i.toString();//test
			}
			return imageArr[i] as ImageContainer;
		}
		public function buildItemArea(_area:int):void{
			this.IB.MIB.loadArea(_area);
			for(var i:int = _area*10;i<=Math.min(this.numItems-1,_area*10+9);i++){
				var _image:ImageContainer = new ImageContainer(this.IB.newImage(),this.IB.newLoadingMovie(),this.IB.newLoadingMovie());
				this.imageArr[i] = _image;
				_image.instance.name = i.toString();//test
				(imageArr[i] as ImageContainer).loadInfo(null);
			}
		}
		public function removeItemArea(_area:int):void{
			for(var i:int = _area*10;i<=Math.min(this.numItems-1,_area*10+9);i++){
				if(this.imageArr[i]){
					(this.imageArr[i] as ImageContainer).loader.unload();
					this.imageArr[i] = null;
				}
			}
		}
		public function addItems(_items:Vector.<MainItem>,_startIndex:int):void{
			for(var i:int=_startIndex;i<=Math.min(this.numItems-1,_startIndex+9);i++){
				//				trace(i,imageArr[i]);
				if(this.imageArr[i]){
					(imageArr[i] as ImageContainer).loadInfo(_items[i-_startIndex]);
				}
			}
		}
		public function destory():void{
			this.IB = null;
			for(var i:int=0;i<this.imageArr.length;i++){
				if(this.imageArr[i]){
					(imageArr[i] as ImageContainer).loader.unload();
				}
			}
			this.imageArr = null;
		}
	}
}